﻿using BNS_Education.IBLL;
using BNS_Education.IDLL;
using BNS_Education.Model;
using BNS_Education.UI.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace BNS_Education.UI.Controllers
{
    public class LoginController : Controller
    {
        private IStaffService StaffService = BNS_Education.BLLContainer.Container.Resolve<IStaffService>();
        // GET: Login
        [HttpGet]
        [AllowAnonymous]
        public ActionResult Login()
        {
            return View();
        }
        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="phone"></param>
        /// <param name="pwd"></param>
        /// <returns></returns>
        [HttpPost]
        [AllowAnonymous]
        public ActionResult Login(string phone, string pwd)
        {

            if (!string.IsNullOrWhiteSpace(phone) && !string.IsNullOrWhiteSpace(pwd))
            {
                User m = new User();
                m.UserPhone = phone;
                m.UserPwd = pwd;
                List<User> list = StaffService.GetModels(p => true).ToList();
                var list1 = list.Where(p => p.UserPhone == m.UserPhone).Count();
                if (list1 > 0)
                {
                    var list2 = list.Where(p => p.UserPhone == m.UserPhone && p.UserPwd == m.UserPwd).Count();
                    //user.Id = Guid.NewGuid().ToString("D");//为了测试手动设置一个用户id
                    FormsAuthHelp.AddFormsAuthCookie(m.UserPhone, m, 60);//设置ticket票据的名称为用户的id，设置有效时间为60分钟
                    if (list2 > 0)
                    {
                        //return Content("<script>location.href='/HomePage/index'</script>");
                        return Json(1);
                    }
                    else
                    {
                        return Json(0);
                    }
                }
                else
                {
                    return Json(-2);
                }

            }
            else
            {
                return Json(-1);
            }

        }

        public ActionResult register()
        {
           
            return View();
        }
        [HttpPost]
        public ActionResult register(string phone_ipt, string vcode_ipt, string pwd_ipt, string pwd2_ipt)
        {
          
            ///判断非空
            if (!string.IsNullOrWhiteSpace(phone_ipt) || !string.IsNullOrWhiteSpace(vcode_ipt) || !string.IsNullOrWhiteSpace(pwd_ipt) || !string.IsNullOrWhiteSpace(pwd2_ipt))
            {
                //判断验证两次输入的一样不一样
                if (Session["vcode"].ToString() == vcode_ipt)
                {
                    //判断密码是不是输入的一样
                    if (pwd_ipt == pwd2_ipt)
                    {
                        //转化为类
                        User m = new Model.User();
                        m.UserPhone = phone_ipt;
                        m.UserPwd = pwd_ipt;

                        //判断添加成功
                        if (StaffService.Add(m))
                        {
                            return Json(1);
                        }
                        else
                        {
                            return Json(-4);
                        }
                    }
                    else
                    {
                        return Json(-3);
                    }
                }
                else
                {
                    return Json(-2);
                }
            }
            else
            {
                return Json(-1);
            }

        }
        /// <summary>
        ///短信接口的验证
        /// </summary>
        /// <param name="Phone"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Ynzheng(string Phone)
        {
            //短信接口
            var vcode = Main.Duanxin(Phone);
            Session["vcode"] = vcode;
            return Json(1);
        }
        [HttpPost]
        public ActionResult verification(string Phone)
        {
            if (!string.IsNullOrWhiteSpace(Phone))
            {
                List<User> list = StaffService.GetModels(p => true).ToList();
                var list1 = list.Where(p => p.UserPhone == Phone).Count();
                if (list1 > 0)
                {
                    return Json(1);
                }
                else
                {
                    return Json(0);
                }
            }
            else
            {
                return Json(-1);
            }
        }
        public ActionResult toFindPwd()
        {
            return View();
        }
        [HttpPost]
        public ActionResult toFindPwd(string phone, string vcode_ipt, string pwd_ipt)
        {
            //判断字段不能为空
            if (!string.IsNullOrWhiteSpace(phone) && !string.IsNullOrWhiteSpace(vcode_ipt) && !string.IsNullOrWhiteSpace(pwd_ipt))
            {
                //判断验证码对不对
                if (Session["vcode"].ToString() == vcode_ipt)
                {
                    //根据手机号查询出数据
                    var ids = StaffService.GetModels(p => true).Where(p => p.UserPhone == phone).FirstOrDefault();
                    //判断数据是否等于空
                    if (ids == null)
                    {
                        return Json(-3);
                    }

                    else
                    {
                        User m = new Model.User();
                        m.UserPhone = phone;
                        m.UserPwd = pwd_ipt;
                        m.UserId = ids.UserId;
                        //最后判断是否成功
                        if (StaffService.Update(m))
                        {
                            return Json(1);
                        }
                        else
                        {
                            return Json(0);
                        }
                    }
                }
                else
                {
                    return Json(-2);
                }

            }
            else
            {
                return Json(-1);
            }

        }
    }
}